package com.haier.waterdevice.general;


import com.haier.waterdevice.R;
import com.haier.waterdevice.general.utils.AppUtil;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.SeekBar;


public class TemperatureSeekBar extends SeekBar {

    private Paint paint;

    private Context context;

    public static Typeface numberTypeface;

    private int maxValue;
    private int minValue;

    public void setMaxAndMin(int maxValue, int minValue) {
        this.maxValue = maxValue;
        this.minValue = minValue;

        invalidate();
    }

    public TemperatureSeekBar(Context context) {
        super(context, null);
    }

    public TemperatureSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        numberTypeface = Typeface.createFromAsset(context.getAssets(),
                "fonts/AndroidClock.ttf");
        this.context = context;
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(getResources().getDimension(
                R.dimen.temperature_seek_bar_value));
        paint.setTextAlign(Align.CENTER);
        paint.setColor(Color.GRAY);
        paint.setFakeBoldText(true);
        paint.setTypeface(numberTypeface);
        int paddingTop = AppUtil.dip2px(context,40);
//        int paddingTop = (int) getResources().getDimension(R.dimen.temperature_seek_bar_pading_top);
        setPadding(20, paddingTop, 25, 0);
    }

    public TemperatureSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setTextAlign(Align.CENTER);
        int progress = getProgress();
        int total = getMax();

        int textSize = (int) getResources().getDimension(
                R.dimen.temperature_seek_bar_value);
        int x = (getWidth() - getPaddingLeft() - getPaddingRight()) * progress
                / total;

        if (textSize < 35) {

            int y = getHeight() - 50;

            int symbolX = x + 28;
            int symbolY = y - 11;

            if (progress < 3) {
                paint.setTextAlign(Align.LEFT);
                symbolX = x + 42;
            } else if (progress > (maxValue - minValue - 1)) {
                symbolX = x + 22;
                paint.setTextAlign(Align.RIGHT);
            } else {
                symbolX = x + 29;
                paint.setTextAlign(Align.CENTER);
            }
            canvas.drawText((progress + minValue) + "", x, y, paint);

            // 画摄氏度
            Paint paint2 = new Paint(paint);
            paint2.setTextSize(30);
            canvas.drawText("℃", symbolX, symbolY, paint2);

        } else if (textSize > 40) {

            int y = getHeight() - 80;

            int symbolX = x + 44;
            int symbolY = y - 18;

            if (progress < 3) {
                paint.setTextAlign(Align.LEFT);
                symbolX = x + 57;
            } else if (progress > (maxValue - minValue - 1)) {
                symbolX = x + 32;
                paint.setTextAlign(Align.RIGHT);
            } else {
                symbolX = x + 44;
                paint.setTextAlign(Align.CENTER);
            }
            canvas.drawText((progress + minValue) + "", x, y, paint);

            // 画摄氏度
            Paint paint2 = new Paint(paint);
            paint2.setTextSize(30);
            canvas.drawText("℃", symbolX, symbolY, paint2);
        } else if(textSize == 40){
//            L.e("ondraw!!!!!!!!等于40");

            int y = getHeight() - 80;

            int symbolX = x + 41;
            int symbolY = y - 12;

            if (progress < 3) {
                paint.setTextAlign(Align.LEFT);
//                symbolX = x + 54;
            } else if (progress > (maxValue - minValue - 1)) {
                symbolX = x + 29;
                paint.setTextAlign(Align.RIGHT);
            } else {
                symbolX = x + 41;
                paint.setTextAlign(Align.CENTER);
            }
            canvas.drawText((progress + minValue) + "", x, y, paint);

            // 画摄氏度
            Paint paint2 = new Paint(paint);
            paint2.setTextSize(30);
            canvas.drawText("℃", symbolX, symbolY, paint2);

        }
    }
}
